#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Agresso.Interface.Layer.Domain;
using System.Web;
using Agresso.Interface.Layer.Service;
using System.Collections;

#endregion

namespace Agresso.Interface.Layer.Web
{
    public partial class SoftmarProfitCentreMappingForm : Form, IFindProfitCentreParentForm
    {
        ProfitCentre m_profitCntre;
        UserControlSoftmarProfitCentreMapping m_parent;
        public SoftmarProfitCentreMappingForm(ProfitCentre profitCentre, UserControlSoftmarProfitCentreMapping parent)
        {
            InitializeComponent();
            this.Text = "Profit Centre Agresso - Softmar Mapping";
            m_profitCntre = profitCentre;
            m_parent = parent;

            textBoxCoType.Text = profitCentre.co_type;
            textBoxFullAddress.Text = profitCentre.full_address;
            textBoxShortCode.Text = profitCentre.short_code;
            textBoxCity.Text = profitCentre.city;
            textBoxShortName.Text = profitCentre.short_name;
            textBoxCountry.Text = profitCentre.country;
            textBoxFullName.Text = profitCentre.full_name;
            textBoxAccountCode.Text = profitCentre.account_code;
            textBoxActive.Text = profitCentre.active == "1" ? "TRUE" : "FALSE";

            textBoxAgrDescription.Text = profitCentre.agr_description;
            textBoxAgrDimValue.Text = profitCentre.agr_dim_value;
            textBoxAgrRelValue.Text = profitCentre.agr_rel_value;
            textBoxCLient.Text = profitCentre.client;
            textBoxCCC.Text = profitCentre.ccc_dim_value;
            textBoxExpenseAcc.Text = profitCentre.expense_account;

            if (profitCentre.CommissionManager.profit_centre_id > 0)
            {
                ProfitCentreService service = new ProfitCentreService();
                profitCentre.CommissionManager = service.GetProfitCentreByID(profitCentre.CommissionManager.profit_centre_id);
                textBoxLegalEntity.Text = profitCentre.CommissionManager.agr_description + " [" + profitCentre.CommissionManager.agr_dim_value+"]";
            }

            VesselService vesselService = new VesselService();
            IList vessels = vesselService.FindVesselsByProfitCentreID(profitCentre.profit_centre_id);
            foreach (Vessel v in vessels)
            {
                listBoxVessel.Items.Add(v);
            }
        }

        public SoftmarProfitCentreMappingForm(string profitCentreName)
        {
            InitializeComponent();
            this.Text = "Profit Centre Agresso - Softmar Mapping";
            ProfitCentreService prfCntrSvc = new ProfitCentreService();
            m_profitCntre = prfCntrSvc.GetProfitCentreByFullName(profitCentreName);

            textBoxCoType.Text = m_profitCntre.co_type;
            textBoxFullAddress.Text = m_profitCntre.full_address;
            textBoxShortCode.Text = m_profitCntre.short_code;
            textBoxCity.Text = m_profitCntre.city;
            textBoxShortName.Text = m_profitCntre.short_name;
            textBoxCountry.Text = m_profitCntre.country;
            textBoxFullName.Text = m_profitCntre.full_name;
            textBoxAccountCode.Text = m_profitCntre.account_code;
            textBoxActive.Text = m_profitCntre.active == "1" ? "TRUE" : "FALSE";

            textBoxAgrDescription.Text = m_profitCntre.agr_description;
            textBoxAgrDimValue.Text = m_profitCntre.agr_dim_value;
            textBoxAgrRelValue.Text = m_profitCntre.agr_rel_value;
            textBoxCLient.Text = m_profitCntre.client;
            textBoxCCC.Text = m_profitCntre.ccc_dim_value;
            textBoxExpenseAcc.Text = m_profitCntre.expense_account;

            if (m_profitCntre.CommissionManager.profit_centre_id > 0)
            {
                ProfitCentreService service = new ProfitCentreService();
                m_profitCntre.CommissionManager = service.GetProfitCentreByID(m_profitCntre.CommissionManager.profit_centre_id);
                textBoxLegalEntity.Text = m_profitCntre.CommissionManager.agr_description + " [" + m_profitCntre.CommissionManager.agr_dim_value + "]";
            }

            VesselService vesselService = new VesselService();
            IList vessels = vesselService.FindVesselsByProfitCentreID(m_profitCntre.profit_centre_id);
            foreach (Vessel v in vessels)
            {
                listBoxVessel.Items.Add(v);
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateSubject();
                this.Close();
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void UpdateSubject()
        {
            m_profitCntre.agr_description = textBoxAgrDescription.Text;
            m_profitCntre.agr_dim_value = textBoxAgrDimValue.Text;
            m_profitCntre.agr_rel_value = textBoxAgrRelValue.Text;
            m_profitCntre.client = textBoxCLient.Text;
            m_profitCntre.ccc_dim_value = textBoxCCC.Text;
            m_profitCntre.expense_account = textBoxExpenseAcc.Text;
            m_profitCntre.country = textBoxCountry.Text;
            ProfitCentreService service = new ProfitCentreService();
            service.UpdateProfitCentreMapping(m_profitCntre);
            if (m_parent != null)
                m_parent.UpdateSelectedItem(m_profitCntre);
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void textBoxAgrDescription_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            buttonApply.Enabled = true;
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateSubject();
                buttonApply.Enabled = false;
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void SoftmarVesselMappingForm_Load(object sender, EventArgs e)
        {
            textBoxAgrDescription.Focus();
            textBoxAgrDescription.SelectAll();
        }

        private void buttonFindLegalEntity_Click(object sender, EventArgs e)
        {
                CommissionManagerListForm form = new CommissionManagerListForm(m_profitCntre.CommissionManager, this);
                form.ShowDialog();
        }

        internal void UpdateCommissionManager(ProfitCentre a)
        {
            m_profitCntre.CommissionManager = a;
            textBoxLegalEntity.Text = a.agr_description;
            buttonApply.Enabled = true;
        }

        private void buttonFindCustomer_Click(object sender, EventArgs e)
        {
            FindProfitCentreForm fform = new FindProfitCentreForm(this);
            fform.ShowDialog();
        }

        public string GetProftCentreName()
        {
            return textBoxShortName.Text;
        }

        public void UpdateAgressoProfitCentre(agldimvalue acu)
        {
            textBoxAgrDescription.Text = acu.description;
            textBoxAgrDimValue.Text = acu.dim_value;
            textBoxAgrRelValue.Text = acu.rel_value;
            textBoxCLient.Text = acu.client;
        }
    }
}